<template>
	<view class="page" :style="{minHeight:windowHeight+'px'}">
		<view class="noDriver" v-if="isShow">
			<!-- <text>您暂时未添加司机，请添加司机。</text> -->
		</view>
		<view class="mycar-msg">
			<view class="mycar-msg1">
				<text>车辆类型:</text>
				<picker v-if="car_details.car_type==''" @change="seleceYears" range-key="type_name" :range="selectType" :value="index">
					<input type="text" placeholder="请选择" v-model="addcar_type" disabled="" />
				</picker>
				<picker v-else @change="seleceYears" range-key="type_name" :range="selectType" :value="index">
					<view v-for="(item,index) of selectType" v-if="car_details.car_type == item.type_id">
						<input  type="text" disabled="" v-model="item.type_name" />
					</view>	
				</picker>
			</view>
			<view class="mycar-msg2">
				<text>车牌号:</text>
				<input v-if="car_details.car_number==''" type="text" placeholder="请输入车牌号" v-model="car_details.car_number" />
				<input v-else type="text" placeholder="请输入车牌号" v-model="car_details.car_number" />
			</view>
		</view>
		<view class="upload">
			<text class="title">请上传车辆信息</text>
			<view class="upload1">
				<view class="upload-imgs" @click="chooseImages('car_info1')">
					<image class="upload-imgs1" src="../../../../static/app/upload-bg-7.png"
						v-if="car_details.car_info1=='' "></image>
					<image class="upload-imgs2" src="../../../../static/app/upload-icon.png"
						v-if="car_details.car_info1=='' "></image>
					<image class="upload-imgs1" :src="car_details.car_info1" mode="" v-else></image>
				</view>
				<view class="upload-imgs upload-item2" @click="chooseImages('car_info2')">
					<image class="upload-imgs1" src="../../../../static/app/upload-bg-6.png"
						v-if="car_details.car_info2=='' "></image>
					<image class="upload-imgs2" src="../../../../static/app/upload-icon.png"
						v-if="car_details.car_info2=='' "></image>
					<image class="upload-imgs1" :src="car_details.car_info2" mode="" v-else></image>
				</view>
				<view class="upload-imgs upload-item3" @click="chooseImages('car_info3')">
					<image class="upload-imgs1" src="../../../../static/app/upload-bg-8.png"
						v-if="car_details.car_info3=='' "></image>
					<image class="upload-imgs2" src="../../../../static/app/upload-icon.png"
						v-if="car_details.car_info3=='' "></image>
					<image class="upload-imgs1" :src="car_details.car_info3" mode="" v-else></image>
				</view>
			</view>
			<text class="title title2">请上传车辆照片</text>
			<view class=" upload2">
				<view class="upload-imgs upload2" @click="chooseImages('car_info44')">
					<image class="upload-imgs1" src="../../../../static/app/upload-bg-5.png"
						v-if="car_details.car_info44=='' "></image>
					<image class="upload-imgs2" src="../../../../static/app/upload-icon.png"
						v-if="car_details.car_info44=='' "></image>
					<image class="upload-imgs1" :src="car_details.car_info44" mode="" v-else></image>
				</view>
			</view>
			<text class="title title2">分配司机</text>
			<view class="select">
				<picker v-if="car_details.driver_id==''" class="select-driver" @change="seleceDriver" range-key="realname" :range="selectDriver"
					:value="driverId">
					<input type="text" placeholder="请选择" value="driver" v-model="driver" disabled="" />
					<image src="../../../../static/app/jiaotou2.png"></image>
				</picker>
				<picker v-else class="select-driver" @change="seleceDriver" range-key="realname" :range="selectDriver"
					:value="driverId">
					<view v-for="(item,index) of selectDriver" v-if="car_details.driver_id == item.id">
						<input  type="text" disabled="" v-model="item.realname" />
					</view>					
					<image src="../../../../static/app/jiaotou2.png"></image>
				</picker>				
			</view>
		</view>
		<button class="submit" @click="$noMultipleClicks(formSubmit)">提交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				car_id:'',//车辆id
				car_details:[],//车辆详情
				noClick:true,
				car_details: {
					car_info1: '',//
					car_info2: '',//
					car_info3: '',//
					car_info44: '',//车辆照片
					addcar_type: '', //车辆类型
					driver_id:'',//司机id
					car_number:'',//车牌号码
					car_id:'',
				},
				addcar_type: '', //车辆类型
				addcar_plate: '', //车牌号
				driver: '', //分配司机
				car_info4: [],
				selectType: [],
				selectDriver: [],
				index: '',
				driverId: '',
				type_id: '', //类型ID
				driver_id: '', //司机ID
				windowHeight: '',
				isShow:false,
			};
		},
		onLoad(e) {
			if(e.select == 1){
				this.car_id = e.car_id;
				this.car_details.car_id = e.car_id;
				this.getCar_detail();
			}
			uni.getSystemInfo({
				success: (res) => {
					this.windowHeight = res.windowHeight;
				}
			});
			this.getCar_type();
			this.getDriver();
		},
		methods: {

			/*司机选择 */
			seleceDriver: function(e) {
				let driverId = e.detail.value;
				let type = this.selectDriver[driverId];
				this.driver = type.realname;
				this.driverId = e.detail.value;
				this.driver_id = type.id
				this.car_details.driver_id = type.id
			},
			/*车辆类型选择 */
			seleceYears: function(e) {
				let index = e.detail.value;
				let type = this.selectType[index];
				this.addcar_type = type.type_name;
				this.index = e.detail.value;
				this.type_id = type.type_id;
				this.car_details.car_type = type.type_id;
			},
			/*获取司机*/
			getDriver() {
				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				self._get('user.index/my_driver', {}, function(res) {
					//console.log("11")
					//console.log(res);
					self.selectDriver = res.data.driver.data;
					if (self.selectDriver.length == 0) {
						self.isShow = true;
						uni.showToast({
							title: '您暂时未添加司机，请添加司机。',
							icon: 'none',
							duration: 2000
						});
						setTimeout(() => {
							self.gotoPage('/pages/user/add-driver/add-driver')
						}, 2000);
					}
					self.loadding = false;
					uni.hideLoading();
				});
			},
			getCar_type() {
				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				self._get('home/getcartype', {}, function(res) {
					self.selectType = res.data.car_type;

					uni.hideLoading();
				});
			},
			/* 获取车辆详情 */
			getCar_detail() {
				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				self._get('car.index/car_details', {car_id:self.car_id}, function(res) {
					self.car_details = res.data.info;
					self.car_details.car_info1 = res.data.info.car_info1;
					self.car_details.car_info2 = res.data.info.car_info2;
					self.car_details.car_info3 = res.data.info.car_info3;
					self.car_details.car_info44 = res.data.info.car_info4;
					self.car_details.addcar_plate = res.data.info.car_number;
					self.type_id = res.data.info.car_type;
					self.driver_id = res.data.info.driver_id;
					uni.hideLoading();
				});
			},
			gotoPage(path) {
				let self = this;
				if (path.startsWith('/')) {
					uni.navigateTo({
						url: path
					});
				} else {
					self[path]();
				}
			},
			chooseImages(type) {
				let self = this;

				// 上传图片idcard_pic1
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: (res) => {
						self.uploadFile(res.tempFilePaths[0], type);
					},
				});
			},
			delect(index) {
				uni.showModal({
					title: "提示",
					content: "是否要删除该图片",
					success: (res) => {
						if (res.confirm) {
							this.car_info4.splice(index, 1)
						}
					}
				})
			},
			uploadFile: function(filePath, type) {

				let self = this;
				let i = 0;
				let params = {
					token: uni.getStorageSync('token'),
					app_id: self.getAppId()
				};
				uni.showLoading({
					title: '图片上传中'
				});
				//console.log(filePath);
				uni.uploadFile({
					url: self.websiteUrl + '/index.php?s=/api/file.upload/image',
					filePath: filePath,
					name: 'iFile',
					formData: params,
					success: function(res) {
						let result = typeof res.data === 'object' ? res.data : JSON.parse(res.data);
						if (result.code === 1) {
							let file_path = result.data.file_path;
							//self.imageList[type] = file_path;
							if (type == 'car_info4') {
								self.car_info4 = self.car_info4.concat(file_path);
							} else {
								self.car_details[type] = file_path;
							}
						}
					},
					complete: function() {
						i++;
						uni.hideLoading();
					}
				});
			},
			formSubmit() {
				let self = this;
				let formdata = {};

				if (self.car_details.car_number == '') {
					uni.showToast({
						title: '请输入车牌号',
						duration: 2000,
						icon: 'none'
					});
					return;
				}
				let regExp = /^([京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新][A-Za-z][1-9DF][1-9A-Za-z]\d{3}[1-9DF]|[京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新][A-Ya-y][\dA-Za-z]{5})$/
				// let regExp = /(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Za-z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$)/
				if (!regExp.test(self.car_details.car_number)) {
					uni.showToast({
						title: '车牌号有误,请重填！',
						duration: 2000,
						icon: 'none'
					});
					return;
				}
				if (self.car_details.car_info1 == '') {
					uni.showToast({
						title: '请上传行驶证印章页',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (self.car_details.car_info2 == '') {
					uni.showToast({
						title: '请上传行驶证条形码',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (self.car_details.car_info3 == '') {
					uni.showToast({
						title: '上传车辆营运证',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (self.car_details.car_info4 == '') {
					uni.showToast({
						title: '请上传车辆照片',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				if (self.car_details.driver_id == '') {
					uni.showToast({
						title: '请选择司机',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				formdata.car_info1 = self.car_details.car_info1;
				formdata.car_info2 = self.car_details.car_info2;
				formdata.car_info3 = self.car_details.car_info3;
				formdata.car_info4 = self.car_details.car_info44;
				formdata.addcar_plate = self.car_details.car_number;
				formdata.addcar_type = self.type_id;
				formdata.car_id = self.car_id;
				//formdata.type_id = self.index;
				formdata.distribution = self.driver_id;
				//formdata = self.car_details;
				uni.showLoading({
					title: '正在提交',
				});
				self._post(
					'user.add/edit_mycar',					
					formdata,
					result => {
						// 记录token user_id
						// uni.setStorageSync('token', result.data.token);
						// uni.setStorageSync('user_id', result.data.user_id);
						if (result.code == 1) {
							uni.showToast({
								title: '编辑成功！',
								duration: 2000,
								icon: 'none'
							});
							self.gotoPage('/pages/user/my-car/my-car');
						}
						// 执行回调函数
						// uni.navigateBack();
					},

					false,
					() => {
						uni.hideLoading();
					}
				)
			}
		}
	}
</script>

<style lang="scss">
	@import 'add-mycar.scss';
</style>
